package com.hushao.lims.weblims.domain;

import com.alibaba.fastjson.annotation.JSONField;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 930588706
 * @date 18/1/13
 * 教师表
 */
@Entity
public class TeacherInfo implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer teacherId;
    /**
     * 教师工号，长度20，不能为空，不能重复
     */
    @NotNull(message = "教师工号不能为空")
    @Column(length = 20,nullable = false,unique = true)
    private String teacherNumber;
    /**
     * 教师头像
     */
    private String teacherPicture;
    /**
     * 教师姓名
     */

    @NotNull(message = "姓名不能为空")
    @Column(length = 50,nullable = false)
    private String teacherName;

    /**
     * 所属院部
     */
    @ManyToOne
    @JoinColumn(name = "department_id",nullable = false)
    private DepartmentInfo departmentInfo;

    /**
     * 教师手机号，不能为空，不能重复
     */
    @NotNull(message = "手机号码不能为空")
    @Pattern(regexp = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$",message = "手机号码格式错误")
    @Column(length = 11,nullable = false,unique = true)
    private String teacherPhone;
    /**
     * 登录密码：不能为空，长度不可小于6
     */
    @Size(min = 6)
    @Column(length = 128,nullable = false)
    private String password;

    /**
     * 创建时间：数据库自动生成
     */
    @Column(nullable = false)
    @CreationTimestamp
    private Timestamp teacherCreateTime;

    /**
     * 角色
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "teacher_role",
            joinColumns = @JoinColumn(name ="teacher_id",referencedColumnName ="teacherId" ),
            inverseJoinColumns = @JoinColumn(name = "role_id",referencedColumnName = "roleId"))
    private List<Role> roles;
    /**
     * 教师间关系表
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "teacher_amity",
            joinColumns = @JoinColumn(name = "teacher_id",referencedColumnName = "teacherId",nullable = false),
            inverseJoinColumns =@JoinColumn(name = "amity_teacher_id",referencedColumnName = "teacherId",nullable = false) )
    private List<TeacherInfo> teacherInfos;

    public TeacherInfo(){}

    public TeacherInfo(String teacherNumber, String teacherPicture, String teacherName, DepartmentInfo departmentInfo, String teacherPhone, List<Role> roles) {
        this.teacherNumber=teacherNumber;
        this.teacherPicture = teacherPicture;
        this.teacherName = teacherName;
        this.departmentInfo = departmentInfo;
        this.teacherPhone = teacherPhone;
        this.roles=roles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> simpleGrantedAuthorities=new ArrayList<>();
        for(Role role:this.roles){
            simpleGrantedAuthorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return simpleGrantedAuthorities;
    }


    @Override
    public String getUsername() {
        return teacherNumber;
    }

    /**
     * 用户账号是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 用户账号是否被锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 用户密码是否过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 用户是否可用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    public Integer getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Integer teacherId) {
        this.teacherId = teacherId;
    }

    public String getTeacherNumber() {
        return teacherNumber;
    }

    public void setTeacherNumber(String teacherNumber) {
        this.teacherNumber = teacherNumber;
    }

    public String getTeacherPicture() {
        return teacherPicture;
    }

    public void setTeacherPicture(String teacherPicture) {
        this.teacherPicture = teacherPicture;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public DepartmentInfo getDepartmentInfo() {
        return departmentInfo;
    }

    public void setDepartmentInfo(DepartmentInfo departmentInfo) {
        this.departmentInfo = departmentInfo;
    }

    public String getTeacherPhone() {
        return teacherPhone;
    }

    public void setTeacherPhone(String teacherPhone) {
        this.teacherPhone = teacherPhone;
    }

    @Override
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * 加密密码
     * @param teacherPassword
     */
    public void setEncodePassword(String teacherPassword) {
        PasswordEncoder encoder=new BCryptPasswordEncoder();
        String encodePasswd=encoder.encode(teacherPassword);
        this.password = encodePasswd;
    }

    public Timestamp getTeacherCreateTime() {
        return teacherCreateTime;
    }

    public void setTeacherCreateTime(Timestamp teacherCreateTime) {
        this.teacherCreateTime = teacherCreateTime;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public List<TeacherInfo> getTeacherInfos() {
        return teacherInfos;
    }

    public void setTeacherInfos(List<TeacherInfo> teacherInfos) {
        this.teacherInfos = teacherInfos;
    }


}
